package com.example.x.dnuifood_929dx.controller;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.x.dnuifood_929dx.R;
import com.example.x.dnuifood_929dx.bean.ResultBean;
import com.example.x.dnuifood_929dx.listener.onRetrofitListener;
import com.example.x.dnuifood_929dx.model.UserModel;

/**
 * Created by X on 2018/10/22.
 */

public class ReginsterActivity extends BaseActivity implements
onRetrofitListener<ResultBean>{

    private Intent intent;
    private Button register;
    private UserModel userModel;
    private EditText et_reg_user,et_reg_password, et_reg_repassword,et_reg_adress,et_reg_telephone,et_reg_comment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }
    @Override
    public void initViews() {

        layout_file= R.layout.activity_reginster;
        setLayout(layout_file);
        register=(Button)findViewById(R.id.register);
        et_reg_user=(EditText)findViewById(R.id.et_reg_user);
        et_reg_password=(EditText)findViewById(R.id.et_reg_password);
        et_reg_repassword=(EditText)findViewById(R.id.et_reg_repassword);
        et_reg_telephone=(EditText)findViewById(R.id.et_reg_telephone);
        et_reg_adress=(EditText)findViewById(R.id.et_reg_adress);
        et_reg_comment=(EditText)findViewById(R.id.et_reg_comment);
    }

    @Override
    public void initEvents() {
        //处理注册按钮
        register.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // bundle.putString ("data", "test");

                if (et_reg_password.getText().toString().equals(et_reg_repassword.getText().toString())) {
                    Intent intent = new Intent(ReginsterActivity.this, LoginActivity.class);
                    intent.putExtra("user", et_reg_user.getText().toString());
                    setResult(RESULT_OK, intent);
                    finish();
                    //register();
                }
                else {
                    //showToast("注册成功");
                    Toast.makeText(ReginsterActivity.this,"请确认两次密码相同",Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    private void register() {
        userModel=new UserModel();
        userModel.register(et_reg_user.getText().toString(),
                et_reg_password.getText().toString(),
                et_reg_telephone.getText().toString(),
                et_reg_adress.getText().toString(),
                et_reg_comment.getText().toString(),this);
    }

    @Override
    public void initData() {

    }



    @Override
    public void onSuccess(ResultBean resultBean, int flag) {
   if(resultBean.getSuccess().equals("1")){
       intent = new Intent();
       intent.putExtra("user",et_reg_user.getText().toString());
       setResult(RESULT_OK,intent);
       finish();
       showToast("注册成功");
   }
   else {
       showToast("注册失败");
   }
    }

    @Override
    public void onFailure(String msg) {
        showToast("网络发生错误");
    }



}
